package ui.activity.custom.gallery;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.trek2000.android.enterprise.R;

import ui.fragment.custom.gallery.AlbumInsideFragment;
import ui.fragment.custom.gallery.AlbumListFragment;
import utils.Utils;

/**
 * Created by trek2000 on 25/2/2015.
 */
public class CustomGallery extends FragmentActivity
        implements View.OnClickListener {

    /**
     * String section
     */
    public static boolean IS_BACK_FROM_CUSTOM_GALLERY_PAGE = false;

    /**
     * View section
     */
    private ImageButton mIbtnBack;
	public static LinearLayout mLlAlbumName;
    public static TextView mTvAlbumName;
    public static TextView mTvUpload;

    /**
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ibtn_back_in_activity_custom_gallery:
                /**todo
                 * Need check current page is which page for us
                 * to know need click Back to which page
                 * - In Album List page, should clear selected files list & close activity
                 * - In Album Inside page, should clear selected files list & go to previous page
                 */
                if (!AlbumInsideFragment.mAlSelectedIndex.isEmpty())
                    AlbumInsideFragment.mAlSelectedIndex.clear();

                if (AlbumListFragment.IS_IN_ALBUM_LIST_FRAGMENT)
                    finish();
                else if (AlbumInsideFragment.IS_IN_ALBUM_INSIDE_PAGE)
                    Utils.clearOldBackStack(this);
                break;
            case R.id.tv_take_new:
                // todo Go to Custom Camera page.
                break;
            case R.id.tv_upload_in_activity_custom_gallery:
                // After selected files in Folder page,
                // begin upload after closed Activity Custom Gallery
                finish();

                // transfer Array List had selected files inside to Enterprise activity
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_custom_gallery);

		/**
         * Set Orientation for page
         */
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        IS_BACK_FROM_CUSTOM_GALLERY_PAGE = false;

        // Should show Fragment Custom Gallery firstly
        getSupportFragmentManager().beginTransaction().replace(
                R.id.fl_in_activity_custom_gallery, AlbumListFragment.newInstance())
                .commitAllowingStateLoss();

        initialViews();
        initialData();

        // Always set default name of Title is "Albums"
        mTvAlbumName.setText(getString(R.string.albums));

		// Clear selected index list from Album Inside fragment
        AlbumInsideFragment.mAlSelectedIndex.clear();
    }

	@Override
    protected void onDestroy() {
        super.onDestroy();

        IS_BACK_FROM_CUSTOM_GALLERY_PAGE = true;
    }

    /**
     * Initial methods
     */

    private void initialData() {
        // Set listener
		mIbtnBack.setOnClickListener(this);
        mTvUpload.setOnClickListener(this);
    }

    private void initialViews() {
        mIbtnBack = (ImageButton) findViewById(R.id.ibtn_back_in_activity_custom_gallery);
        mLlAlbumName = (LinearLayout) findViewById(R.id.ll_album_name_in_activity_custom_gallery);
        mTvAlbumName = (TextView) findViewById(R.id.tv_album_name_in_activity_custom_gallery);
        mTvUpload = (TextView) findViewById(R.id.tv_upload_in_activity_custom_gallery);
    }

    /**
     * Basic methods
     */
}
